package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.DefaultRowSorter;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.RowSorter;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import CommonGui.NotEdditibleTable;
import DataObjects.ConstantObjects;

/**
 * Class to show all  business menus
 * @author Andrey,Igor
 *
 */
public class PfSearch_business_menu  extends JPanel {
	
	private DefaultTableModel dtm;
	private MainMenu mainMenu;
	private Vector<Vector> menu;            
	private JTable tbl;
	private JTextField nameTF;
	private JTextField priceMenuTF;
	private RowSorter<DefaultTableModel> sorterRole;
	private RowSorter<DefaultTableModel> sorterName;
	private RowSorter<DefaultTableModel> sorterPriceMenu;
	
	/**
	 * Method-constructor 
	 * @param mm
	 * @param bus_menu - includes details of all business menus
	 */
	public PfSearch_business_menu(MainMenu mm, Vector<Vector> bus_menu)
	{
		super();
		this.setLayout(new BorderLayout());
		this.mainMenu=mm;
		this.menu=bus_menu;             
		initiliaze();
	
	}
	
	private void initiliaze()
	{
		JScrollPane scrollPane = new JScrollPane(getTblMenu());
		this.add(scrollPane,BorderLayout.CENTER);
		this.add(getSearchUnits(),BorderLayout.NORTH);
	}
	
	
	 /**
     * Method for creation JTable tbl with all business menus
     * @return JTable tbl
     */
	private JTable getTblMenu()
	{
		Vector <String> tblheader=new Vector<String>();
		
		tblheader.add("מס' תפריט");
		tblheader.add("שם תפריט");
		tblheader.add("מחיר תפריט");
		tblheader.add("סטטוס");
				
		dtm=new NotEdditibleTable(menu,tblheader);	 
		tbl = new JTable(dtm);
		DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
		centerRenderer.setHorizontalAlignment( JLabel.CENTER );
		for(int x=0;x<4;x++){
			tbl.getColumnModel().getColumn(x).setCellRenderer( centerRenderer );
	        }
		tbl.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		tbl.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		tbl.setFont(new Font("David", Font.BOLD, 16));
		tbl.getTableHeader().setFont( new Font( "Arial" , Font.BOLD, 17 ));
		tbl.getTableHeader().setBackground(new Color(173,216,230));
		tbl.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 
	    tbl.setRowHeight(30);
	    tbl.setFillsViewportHeight(true);
        tbl.setGridColor(new Color(25, 25, 112));
        tbl.setBackground(new Color(250, 235, 215));
        tbl.setForeground(new Color(0, 0, 0));
        tbl.setAutoCreateRowSorter(true);
                
        tbl.addMouseListener(new MouseAdapter() {
			   public void mouseClicked(MouseEvent e) {
			     if (e.getClickCount() == 1) {
			       JTable target = (JTable)e.getSource();
			       int row = target.getSelectedRow();
			       int column = target.getSelectedColumn();
			       // do some action if appropriate column
			       if(row!=-1)
			       {
			       mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.EAST));
			       int row_index=target.getSelectedRow();
			       Vector<String> vec=(Vector<String>) dtm.getDataVector().get(row_index);
			       mainMenu.getContentPane().add(new PanelFunctionMenuListAllBusMenu(mainMenu,vec), BorderLayout.EAST);
			       mainMenu.getContentPane().validate();
			       }
			     }
			   }
			 });
        
	
        return tbl;
	}

	/**
	 * Method for creation JPanel searchUnits with some components.It gets textField PriceMenuTF 
	 * and PriceMenuLbl   from method getPriceMenuTF and method getPriceMenuLbl
	 * for quick search any recipe by price.Also it gets NameTF and NameLbl from 
	 * getNameTF and getNameLbl for quick search by a name.Also it gets RolesCB
	 * and RoleLbl from methods getRolesCB and getRoleLbl (filters)
	 * @return
	 */
	private JPanel getSearchUnits()
	{
		JPanel searchUnits=new JPanel ();
		searchUnits.setBackground(new Color(255, 245, 238));
		searchUnits.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		searchUnits.add(getNameTF());
		searchUnits.add(getNameLbl());
		searchUnits.add(getPriceMenuTF());
		searchUnits.add(getPriceMenuLbl());
		searchUnits.add(getRolesCB());
		searchUnits.add(getRoleLbl());
		return searchUnits;
	}
	
	/**
	 * Method for creation JTextField nameTF with  KeyListener keyListener which active filter by name business menu
	 * @return JTextField nameTF
	 */
	private JTextField getNameTF()
	{
		nameTF=new JTextField(20);
		nameTF.setHorizontalAlignment(JTextField.RIGHT);
		KeyListener keyListener = new KeyListener() 
		{		      
			public void keyPressed(KeyEvent arg0) {}

			public void keyReleased(KeyEvent arg0) 
			{
				JTextField nameField = (JTextField)arg0.getSource();
				String selectedName = (String)nameField.getText();
				filterByName(selectedName);				
			}

			public void keyTyped(KeyEvent arg0) {}		
		};
		
		nameTF.addKeyListener(keyListener);		
		return nameTF;
	}
	
	/**
	 * Method for creation TableRowSorter sorterName.Used by keyListener of JTextField nameTF
	 * @param name - name of menu
	 */
	private void filterByName(String name)
	{
		sorterName = new TableRowSorter<DefaultTableModel>(dtm);     		   
	    tbl.setRowSorter(sorterName);  	    
	    RowFilter< DefaultTableModel  , Object> rf = null;   
	    //declare a row filter for your table model  
	    try  
	    {  
	        rf = RowFilter.regexFilter(name, 1);    
	        //initialize with a regular expression  
	    }  
	   catch (java.util.regex.PatternSyntaxException e1)  
	    {  
	        return;  
	    }  
	   
	    ((DefaultRowSorter<DefaultTableModel, Integer>) sorterName).setRowFilter(rf);		
	}
	
	/**
	 * Method for creation JTextField PriceMenuTF with  KeyListener keyListener which active filter by price business menu
	 * @return JTextField priceMenuTF
	 */
	private JTextField getPriceMenuTF()
	{
	    priceMenuTF=new JTextField(20);
	    priceMenuTF.setHorizontalAlignment(JTextField.RIGHT);
	    
	    KeyListener keyListener = new KeyListener() 
		{		      
			public void keyPressed(KeyEvent arg0) {}

			public void keyReleased(KeyEvent arg0) 
			{
				// TODO Auto-generated method stub
				JTextField lastNameField = (JTextField)arg0.getSource();
				String selectedLastName = (String)lastNameField.getText();
				filterByPriceMenu(selectedLastName);				
			}

			public void keyTyped(KeyEvent arg0) {}		
		};
		
		priceMenuTF.addKeyListener(keyListener);		
		return priceMenuTF;
	}
	
	/**
	 * Method for creation TableRowSorter sorterPriceMenu.Used by keyListener of JTextField priceMenuTF
	 * @param name - price of menu
	 */
	private void filterByPriceMenu(String priceMenu)
	{
		sorterPriceMenu = new TableRowSorter<DefaultTableModel>(dtm);     		   
	    tbl.setRowSorter(sorterPriceMenu);  	    
	    RowFilter< DefaultTableModel  , Object> rf = null;   
	    //declare a row filter for your table model  
	    try  
	    {  
	        rf = RowFilter.regexFilter(priceMenu, 2);    
	        //initialize with a regular expression  
	    }  
	   catch (java.util.regex.PatternSyntaxException e1)  
	    {  
	        return;  
	    }  
	   
	    ((DefaultRowSorter<DefaultTableModel, Integer>) sorterPriceMenu).setRowFilter(rf);		
	}
	
	
	
	
	/**
	 * Method for creation JComboBox rolesCB which active filter by role
	 * @return JComboBox rolesCB
	 */
	private JComboBox getRolesCB()
	{
		JComboBox rolesCB=new JComboBox(new ConstantObjects().getMenuStatus());
		rolesCB.addActionListener(new java.awt.event.ActionListener() 
	  	{
			public void actionPerformed(java.awt.event.ActionEvent e) {
				//nameTF.setText(null);
				//lastNameTF.setText(null);
				JComboBox roleBox = (JComboBox)e.getSource();
			    String selectedRole = (String)roleBox.getSelectedItem(); 
			    filterByRole(selectedRole);
				} 
		});	
		return rolesCB;
	}
	
	/**
	 * Method for creation TableRowSorter sorterRole.Used by JComboBox roleCB
	 * @param name - role of menu
	 */
	private void filterByRole(String selRole)
	{
		if(selRole.equals("בחר סטטוס"))
		{
			((DefaultRowSorter<DefaultTableModel, Integer>) sorterRole).setRowFilter(null);
		}
		
		else 
		{
		sorterRole = new TableRowSorter<DefaultTableModel>(dtm);     		   
	    tbl.setRowSorter(sorterRole);  	    
	    RowFilter< DefaultTableModel  , Object> rf = null;   
	    //declare a row filter for your table model  
	    try  
	    {  
	        rf = RowFilter.regexFilter("^" + selRole, 0);    
	        //initialize with a regular expression  
	    }  
	   catch (java.util.regex.PatternSyntaxException e1)  
	    {  
	        return;  
	    }  
	   
	    ((DefaultRowSorter<DefaultTableModel, Integer>) sorterRole).setRowFilter(rf);
		}   //else
	}
	
	/**
	 * Three methods for creation labels to filters by name and by price and by role of 
     *  business menus
	 * @return JLabel nameLbl and JLabel priceMenuLbl
	 */
	private JLabel getNameLbl()
	{
		JLabel nameLbl=new JLabel("  שם :  ");
		nameLbl.setFont(new Font("Tahoma", Font.PLAIN, 13));
		return nameLbl;
	}
	
	private JLabel getPriceMenuLbl()
	{
		JLabel priceMenuLbl=new JLabel(" מחיר  :  ");
		priceMenuLbl.setFont(new Font("Tahoma", Font.PLAIN, 13));
		return priceMenuLbl;
	}
	private JLabel getRoleLbl()
	{
		JLabel roleLbl=new JLabel("   סטטוס  :  ");
		roleLbl.setFont(new Font("Tahoma", Font.PLAIN, 13));
		return roleLbl;
	}
}

